# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=pcre
pkgname=('pcre' 'libpcre' 'libpcre16' 'libpcre32' 'libpcrecpp' 'libpcreposix' 'pcre-devel')
pkgver=8.44
pkgrel=1
pkgdesc="A library that implements Perl 5-style regular expressions"
arch=('i686' 'x86_64')
url="https://www.pcre.org/"
license=('BSD')
makedepends=('libreadline-devel' 'libbz2-devel' 'zlib-devel')
source=(https://ftp.pcre.org/pub/pcre/${pkgname}-${pkgver}.tar.bz2{,.sig}
        pcre-8.33-msys2-fix-ln.patch
        pcre-8.33-msys2.patch)
sha256sums=('19108658b23b3ec5058edc9f66ac545ea19f9537234be1ec62b714c84399366d'
            'SKIP'
            '59d420f6e48f527a464902913a101be27297fb26318d532f67ec0a245ea39db8'
            '26e0787a12ff5e8015773b318172166f541eb94d4f103989f51866672637b949')
validpgpkeys=('45F68D54BBE23FB3039B46E59766E084FB0F43D8')

prepare() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/pcre-8.33-msys2-fix-ln.patch
  patch -p1 -i ${srcdir}/pcre-8.33-msys2.patch

  autoreconf -fi
}

build() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  ./configure --build=${CHOST} \
      --prefix=/usr \
      --enable-pcre16 \
      --enable-pcre32 \
      --enable-newline-is-anycrlf \
      --enable-unicode-properties \
      --enable-utf8 \
      --enable-pcregrep-libbz2 \
      --enable-pcregrep-libz \
      --enable-pcretest-libreadline \
      --disable-stack-for-recursion

  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  make -j1 check
}

package_pcre() {
  depends=('libreadline' 'libbz2' 'zlib' 'libpcre' 'libpcre16' 'libpcre32' 'libpcrecpp' 'libpcreposix')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/LICENCE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_libpcre() {
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre-1.dll ${pkgdir}/usr/bin/
}

package_libpcre16() {
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre16-0.dll ${pkgdir}/usr/bin/
}

package_libpcre32() {
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre32-0.dll ${pkgdir}/usr/bin/
}

package_libpcrecpp() {
  groups=('libraries')
  depends=('libpcre' 'gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcrecpp-0.dll ${pkgdir}/usr/bin/
}

package_libpcreposix() {
  depends=('libpcre')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcreposix-0.dll ${pkgdir}/usr/bin/
}

package_pcre-devel() {
  pkgdesc="PCRE headers and libraries"
  options=('staticlibs')
  groups=('development')
  depends=("libpcre=$pkgver" "libpcre16=$pkgver" "libpcre32=$pkgver" "libpcreposix=$pkgver" "libpcrecpp=$pkgver")

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
